<template>
  <div class="registerBody">
    <div class="registerDiv">
      <div class="register-content">
        <h1 class="register-title">注册</h1>

        <el-form :model="readerForm" label-width="100px" :rules="rules" ref="readerForm">

          <el-form-item label="昵称" prop="realname">
            <el-input style="width: 200px" prefix-icon="el-icon-user" v-model="readerForm.realname"  size="small"></el-input>
          </el-form-item>

          <el-form-item label="账号" prop="username">
            <el-input style="width: 200px" prefix-icon="el-icon-user-solid" v-model="readerForm.username"  size="small"></el-input>
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input style="width: 200px"  show-password prefix-icon="el-icon-lock" v-model="readerForm.password" size="small" ></el-input>
          </el-form-item>

          <el-form-item label="电话" prop="tel">
            <el-input style="width: 200px" prefix-icon="el-icon-phone" v-model="readerForm.tel" size="small" ></el-input>
          </el-form-item>

          <el-form-item label="性别"  prop="sex" style="width: 300px">
            <el-select v-model="readerForm.sex" placeholder="请选择性别">
              <el-option label="男" value="男"></el-option>
              <el-option label="女" value="女"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item class="el-form-item--right">
            <el-button type="primary" round @click="submitForm">注册</el-button>
            <el-button type="info" round @click="resetForm">重置</el-button>
          </el-form-item>

          <el-form-item class="el-form-item--right-register">
            <router-link to="/login">已有账号，去登录</router-link>
          </el-form-item>
          
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "Register",
  data(){
    var validateRegisterUsername = async (rule, value, callback) => {
      const {data:res} = await this.$axios.get("login/checkRegisterUsername",{params:{username:this.readerForm.username}})
      if (res.data==null){
        callback();
      }else if (value===res.data.username){
        callback(new Error('账号已存在'));
      }else{
        callback();
      }
    };
    var validateRegisterRealname = async (rule, value, callback) => {
      const {data:res} = await this.$axios.get("login/checkRegisterRealname",{params:{realname:this.readerForm.realname}})
      if (res.data==null){
        callback();
      }else if (value===res.data.realname){
        callback(new Error('昵称已存在'));
      }else{
        callback();
      }
    };
    return{
      readerForm:{
        readernumber: this.generateRandomNumber(),
        username: '',
        realname: '',
        sex: '',
        tel: '',
        registerdate: '',
        email: '',
        birthday:'',
        role:'',
        password:'',
        numberborrow:'',
        icon:''
      },
      rules:{
        realname: [
          { required: true, message: '请输入昵称', trigger: 'blur' },
            {validator:validateRegisterRealname, trigger: 'change'}
        ],
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
            {validator:validateRegisterUsername, trigger: 'change'}
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        tel: [
          { required: true, message: '请输入电话', trigger: 'blur' },
        ],
        sex: [
          { required: true, message: '请选择性别',trigger: 'blur'}
        ]
      }
    }
  },
  methods:{
    submitForm() {
      this.$refs.readerForm.validate((valid) => {
        if (valid) {
          this.readerForm.role = '0';
          this.readerForm.icon = 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'
          this.readerForm.registerdate = this.getCurrentDate();
          this.readerForm.numberborrow = '0';
          axios.post('/login/addRegisterReader', this.readerForm)
              .then(response => {
                this.$message.success('注册成功');
                this.$router.push('/login');
              })
        } else {
          this.$message.error('注册失败');
          return false;
        }
      });
    },
    generateRandomNumber() {
      // 生成一个10位的随机数字
      return Math.floor(1000000000 + Math.random() * 9000000000).toString();
    },
    getCurrentDate() {
      const now = new Date();
      return now.toISOString();
    },
    resetForm(){
      this.$refs.readerForm.resetFields();
      this.readerForm.readernumber = this.generateRandomNumber();
    }
  }
}
</script>

<style scoped>
.registerBody {
  position: absolute;
  width: 100%;
  height: 100%;
  background: linear-gradient(to right,pink,skyblue);
  background-size: 100% 100%;
}
.registerDiv {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -230px;
  margin-left: -220px;
  /*width: 450px;*/
  height: 360px;
  background: #fff;
  border-radius: 5%;
}
.register-title {
  margin: 20px 0;
  text-align: center;
  background: linear-gradient(to right, #ff7e5f, #feb47b); /* 渐变颜色 */
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}
.register-content {
  width: 400px;
  height: 250px;
  position: absolute;
  top: 25px;
  left: 25px;
}
.el-form-item--right {
  margin-left: 38px;
}
.el-form-item--right-register {
  margin-left: 42px;
}
</style>